package com.example.administrator.dc.Activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.administrator.dc.R;
import com.example.administrator.dc.fragment.RegFragmentone;
import com.example.administrator.dc.fragment.RegFragmentthree;
import com.example.administrator.dc.fragment.RegFragmenttwo;

import java.util.ArrayList;
import java.util.List;

public class RegActivity extends AppCompatActivity {
    private RadioButton first;
    private RadioButton second;
    private RadioButton three;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private RegFragmentone regFragmentone;
    private RegFragmenttwo regFragmenttwo;
    private RegFragmentthree regFragmentthree;
    private ViewPager vp;
    SharedPreferences preferences;

    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        init();
        initEvents();
        initFragment();
        first.setVisibility(View.GONE);
        second.setVisibility(View.GONE);
        three.setVisibility(View.GONE);
    }
    private  void init(){
        first= (RadioButton) findViewById(R.id.first);
        second= (RadioButton) findViewById(R.id.second);
        three= (RadioButton) findViewById(R.id.three);
        //  radioButton3= (RadioButton) findViewById(R.id.radioButton3);
        vp= (ViewPager) findViewById(R.id.viewPager);


        fragmentManager=getSupportFragmentManager();      //getChildFragmentManager();在fragment

        fragmentTransaction=fragmentManager.beginTransaction();
        regFragmentone=new RegFragmentone();
        regFragmenttwo=new RegFragmenttwo();
        regFragmentthree=new RegFragmentthree();

          // threeFragment=new ThreeFragment();
        //  fourFragment=new FourFragment();

        fragmentTransaction.add(R.id.viewPager,regFragmentone);
        fragmentTransaction.add(R.id.viewPager,regFragmenttwo);
        fragmentTransaction.add(R.id.viewPager,regFragmentthree);
        //fragmentTransaction.add(R.id.viewPager,threeFragment);
        //fragmentTransaction.add(R.id.framelayout,fourFragment);

        fragmentTransaction.commit();

    }

    private  void initEvents(){

        first.setOnClickListener(onClickListener);
        second.setOnClickListener(onClickListener);
        three.setOnClickListener(onClickListener);
        //    radioButton3.setOnClickListener(onClickListener);


    }
    View.OnClickListener  onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.first:
                    Toast.makeText(RegActivity.this, "第一步", Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.second:
                    Toast.makeText(RegActivity.this, "第二步", Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.three:
                    Toast.makeText(RegActivity.this,"第三步",Toast.LENGTH_LONG).show();
                    selectTab(2);
                    break;
            }



        }
    };

    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                //字体颜色
                break;
            case 1:

                break;
            case 2:

                break;
            case 3:

                break;
            case 4:

                break;
            case 5:

        }
        vp.setCurrentItem(i);
    }

    private void initFragment(){
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new RegFragmentone());
        listfragment.add(new RegFragmenttwo());
        listfragment.add(new RegFragmentthree());
        //    listfragment.add(new ThreeFragment());

        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp.setAdapter(fpadapter);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                System.out.println("----"+String.valueOf(position));
                int i = Integer.parseInt(String.valueOf(position));
                vp.setCurrentItem(position);
                if(i==0){
                    fragmentManager=getSupportFragmentManager();
                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.show(regFragmentone);
                    fragmentTransaction.hide(regFragmenttwo);
                    fragmentTransaction.hide(regFragmentthree);
                    //    fragmentTransaction.hide(threeFragment);
                    fragmentTransaction.commit();
                }
                if(i==1){
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.show(regFragmenttwo);
                    fragmentTransaction.hide(regFragmentone);
                    fragmentTransaction.hide(regFragmentthree);
                    //   fragmentTransaction.hide(threeFragment);
                    fragmentTransaction.commit();

                }
              if(i==2){

                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.show(regFragmentthree);
                    fragmentTransaction.hide(regFragmenttwo);
                    fragmentTransaction.hide(regFragmentone);
                    fragmentTransaction.commit();

                }

             /*   resetImage();
                resetText();
                selectTab(position);*/
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }



}
